//
//  YunPlatformSet.m
//  reSearchDemo
//
//  Created by 科技 on 15/7/31.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "YunPlatformSet.h"
#import "PlatformType.h"
@interface YunPlatformSet ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
{
    UITableView *_tableView;
    UIView *_headView;
}
@end

@implementation YunPlatformSet

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor=[UIColor whiteColor];
    [self setRightBarButtonImage:LOADIMAGE(@"btn_more") highlightedImage:nil selector:@selector(menu)];
    
    UIView * actionbar=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 44)];
    self.navigationItem.titleView = actionbar;
    UILabel * lab=[[UILabel alloc]initWithFrame:CGRectMake(8, 0, 100, 40)];
    lab.text=@"云平台设置";
    lab.font=[UIFont systemFontOfSize:20];
    lab.textColor=[UIColor whiteColor];
    [actionbar addSubview:lab];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_tableView];
    
    _headView = [self buildMainView];

}

-(void)menu {
    
}

-(UIView *)buildMainView {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
//    headView.backgroundColor = [UIColor lightGrayColor];
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 40, 100, 20)];
    textLabel.text = @"设置平台号:";
    [headView addSubview:textLabel];
    NSArray *titleArr = @[@"平台号:",@"平台名称:"];
    CGSize size1 = [@"平台号:" sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]}];
    CGSize size2 = [@"平台名称:" sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]}];
    for (int i = 0; i < 2; i++) {
        UILabel *label = [[UILabel alloc] init];
        label.text = titleArr[i];
        label.font = [UIFont systemFontOfSize:17];
        
        UITextField *textField = [[UITextField alloc] init];
        textField.tag = 1000+i;
        textField.delegate = self;
        
        UIView *lane = [[UIView alloc] init];
        lane.backgroundColor = [UIColor blackColor];
        if (i == 0) {
            label.frame = CGRectMake(CGRectGetMinX(textLabel.frame), CGRectGetMaxY(textLabel.frame) + 10, size1.width, 30);
            textField.frame = CGRectMake(CGRectGetMaxX(label.frame) + 5, CGRectGetMinY(label.frame), 150, CGRectGetHeight(label.frame));
            lane.frame = CGRectMake(CGRectGetMinX(textField.frame), CGRectGetMaxY(textField.frame) - 5, CGRectGetWidth(textField.frame) , 1);
        } else {
            label.frame = CGRectMake(CGRectGetMinX(textLabel.frame), CGRectGetMaxY(textLabel.frame) + 50, size2.width, 30);
            textField.frame = CGRectMake(CGRectGetMaxX(label.frame) + 5, CGRectGetMinY(label.frame), 150, CGRectGetHeight(label.frame));
            lane.frame = CGRectMake(CGRectGetMinX(textField.frame), CGRectGetMaxY(textField.frame) - 5, CGRectGetWidth(textField.frame), 1);
        }
        
        [headView addSubview:label];
        [headView addSubview:textField];
        [headView addSubview:lane];
    }
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame = CGRectMake(CGRectGetMinX(textLabel.frame), CGRectGetMaxY(textLabel.frame) + 2*50 + 20, CGRectGetWidth(self.view.frame) - 2*CGRectGetMinX(textLabel.frame), 40);
    [sureBtn setTitle:@"确定" forState:0];
    [sureBtn setTitleColor:[UIColor blackColor] forState:0];
    [sureBtn setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
    [sureBtn addTarget:self action:@selector(SureBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:sureBtn];
    
    NSString *promptText = @"备注:平台号设置成功,终身不得修改,平台号必须包含字母,四位字符以上";
    UILabel *promptLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(sureBtn.frame) + 30, CGRectGetWidth(headView.frame) - 40, 40)];
    promptLabel.textColor = [UIColor lightGrayColor];
    promptLabel.numberOfLines = 0;
    promptLabel.text = promptText;
    promptLabel.font = [UIFont systemFontOfSize:13];
    
    [headView addSubview:promptLabel];
    
    return headView;
}
//确认按钮
-(void)SureBtnAction:(UIButton *)sender {
    /*
    UITextField *TF1 = (UITextField *)[self.view viewWithTag:1000];
    UITextField *TF2 = (UITextField *)[self.view viewWithTag:1001];
    if (TF1.text  <= 0 && TF2.text <= 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示:" message:@"请输入内容!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    NSString * regularExpression = @"^(?![0-9]+$)[a-zA-Z0-9]{4,18}";
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regularExpression];
    BOOL flag = [pre evaluateWithObject:TF1.text];
    if (!flag) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示:" message:@"平台号输入有误!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setObject:TF1.text forKey:@"PlatformID"];
    [user synchronize];
     */
    [self pushViewController:[PlatformType new]];
}

#pragma mark -
#pragma mark --textFieldDelegate--
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

#pragma mark -
#pragma mark --tableviewDelegate--
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *indentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = _headView;
    return headView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    UIView *headView = [self tableView:tableView viewForHeaderInSection:section];
    
    return CGRectGetHeight(headView.frame);
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    UITextField *TF = (UITextField *)[self.view viewWithTag:1000];
    UITextField *TF1 = (UITextField *)[self.view viewWithTag:1001];
    [TF1 resignFirstResponder];
    [TF resignFirstResponder];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
